package com.wh.hotel.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wh.common.annotation.ResultLog;
import com.wh.common.enums.MethodTypeEnum;
import com.wh.hotel.cache.HotelPriceStrategyCache;
import com.wh.hotel.constants.CommonConstant;
import com.wh.hotel.enums.YesNoEnum;
import com.wh.hotel.enums.base.HotelOperateTypeEnum;
import com.wh.hotel.mapper.HotelPricingStrategyMapper;
import com.wh.hotel.model.HotelPricingStrategy;
import com.wh.hotel.vo.HotelPriceStrategyVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author luweije
 */
@Component
public class HotelPricingStrategyDao {

    @Resource
    private HotelPricingStrategyMapper hotelPricingStrategyMapper;

    @Resource
    private HotelPriceStrategyCache hotelPriceStrategyCache;

    @ResultLog(name = "HotelPricingStrategyDao.insert", methodType = MethodTypeEnum.DB)
    public boolean insert(HotelPricingStrategy hotelPricingStrategy) {
        try {
            return hotelPricingStrategyMapper.insert(hotelPricingStrategy) >= CommonConstant.SUCCESS_DB;
        } finally {
            // todo mq广播
            hotelPriceStrategyCache.updateById(hotelPricingStrategy.getId());
        }
    }

    @ResultLog(name = "HotelPricingStrategyDao.update", methodType = MethodTypeEnum.DB)
    public boolean update(HotelPricingStrategy hotelPricingStrategy) {
        try {
            return hotelPricingStrategyMapper.updateById(hotelPricingStrategy) >= CommonConstant.SUCCESS_DB;
        } finally {
            // todo mq广播
            hotelPriceStrategyCache.updateById(hotelPricingStrategy.getId());
        }
    }

    @ResultLog(name = "HotelPricingStrategyDao.update", methodType = MethodTypeEnum.DB)
    public boolean updateStrategyStatus(Long id, HotelOperateTypeEnum operateTypeEnum) {
        HotelPricingStrategy updateModel = new HotelPricingStrategy();
        LambdaQueryWrapper<HotelPricingStrategy> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HotelPricingStrategy::getId, id);
        switch (operateTypeEnum) {
            case ON:
                queryWrapper.in(HotelPricingStrategy::getStrategyStatus, YesNoEnum.NO.getCode());
                updateModel.setStrategyStatus(YesNoEnum.YES.getCode());
                break;
            case OFF:
                queryWrapper.eq(HotelPricingStrategy::getStrategyStatus, YesNoEnum.YES.getCode());
                updateModel.setStrategyStatus(YesNoEnum.NO.getCode());
                break;
        }
        try {
            return hotelPricingStrategyMapper.update(updateModel, queryWrapper) >= CommonConstant.SUCCESS_DB;
        } finally {
            // todo mq广播
            hotelPriceStrategyCache.updateById(id);
        }
    }

    @ResultLog(name = "HotelPricingStrategyDao.update", methodType = MethodTypeEnum.DB, printResult = false)
    public IPage<HotelPricingStrategy> selectPageByCondition(HotelPriceStrategyVo hotelPriceStrategyVo) {
        IPage<HotelPricingStrategy> page = new Page<>(hotelPriceStrategyVo.getPageNum(), hotelPriceStrategyVo.getPageSize());
        LambdaQueryWrapper<HotelPricingStrategy> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.likeRight(StringUtils.isNotBlank(hotelPriceStrategyVo.getStrategyName()), HotelPricingStrategy::getStrategyName, hotelPriceStrategyVo.getStrategyName())
                .eq(hotelPriceStrategyVo.getStrategyStatus() != null, HotelPricingStrategy::getStrategyType, hotelPriceStrategyVo.getStrategyStatus())
                .eq(hotelPriceStrategyVo.getId() != null, HotelPricingStrategy::getId, hotelPriceStrategyVo.getId())
                .eq(hotelPriceStrategyVo.getStrategyType() != null, HotelPricingStrategy::getStrategyType, hotelPriceStrategyVo.getStrategyType());
        return hotelPricingStrategyMapper.selectPage(page, queryWrapper);
    }

    @ResultLog(name = "HotelPricingStrategyDao.selectById", methodType = MethodTypeEnum.DB, printResult = false)
    public HotelPricingStrategy selectById(Long strategyId) {
        return hotelPricingStrategyMapper.selectById(strategyId);
    }

    @ResultLog(name = "HotelPricingStrategyDao.queryPageById", methodType = MethodTypeEnum.DB, printResult = false)
    public List<HotelPricingStrategy> queryPageById(long minId, int pageSize) {
        LambdaQueryWrapper<HotelPricingStrategy> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.gt(HotelPricingStrategy::getId, minId);
        queryWrapper.orderByAsc(HotelPricingStrategy::getId);
        queryWrapper.last("limit " + pageSize);
        return hotelPricingStrategyMapper.selectList(queryWrapper);
    }
}
